package com.asa.mvvm_aop_frame.basic.aspect

import android.util.Log
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_DESTROY
import com.asa.mvvm_aop_frame.basic.pointcut.ActivityLifecyclePoint.ON_ACTIVITY_START
import com.asa.mvvm_aop_frame.basic.ui.BaseActivity
import com.gyf.barlibrary.BarHide
import com.gyf.barlibrary.ImmersionBar
import com.gyf.barlibrary.OnKeyboardListener
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.After
import org.aspectj.lang.annotation.Aspect

/**
 * create By：anderson
 * on 2022/4/27
 * desc:
 */
@Aspect
class BarAspect {

    @After(ON_ACTIVITY_START)
    open fun initBarAspect(joinPoint: JoinPoint) {
        Log.e("日志", "initBarAspect: bar start")
        if (joinPoint.`this` is BaseActivity<*, *>) {

            initImmersionBar(joinPoint.`this` as BaseActivity<*, *>)
        }
    }

    @After(ON_ACTIVITY_DESTROY)
    open fun destroyBarAspect(joinPoint: JoinPoint) {
        if (joinPoint.`this` is BaseActivity<*, *>) {
          ImmersionBar.with(joinPoint.`this` as BaseActivity<*, *>)
                .destroy()
        }
    }

    private fun initImmersionBar(activity: BaseActivity<*, *>) {
        if (activity.immersionBarEnabled()) {
            if (activity.isFullScreen()) {
                ImmersionBar.with(activity)
                    .hideBar(BarHide.FLAG_HIDE_BAR)
                    .init()
            } else {
                ImmersionBar.with(activity)
                    .fitsSystemWindows(activity.fitsSystemWindows())
                    .statusBarDarkFont(activity.statusBarDarkFont(), 0.2f)
                    .statusBarColor(activity.statusBarColor())
                    .navigationBarColor(activity.navigationBarColor()).also {
                        if (activity.hideNavigationBar()) {
                            it.hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
                        }
                    }
                    .keyboardEnable(activity.keyboardEnable())
                    .keyboardMode(activity.keyboardMode())
                    .setOnKeyboardListener(OnKeyboardListener { isPopup, keyboardHeight ->
                        activity.onKeyboardChange(isPopup, keyboardHeight)
                    })
                    .init()
            }
        }
    }
}